<?PHP
	require 'includes/master.inc.php';
	require 'includes/class.googlechart.php';
	$auth->admin();

	$app_where = '';
	if(isset($_GET['app_id']))
	{
		$app = new App($_GET['app_id']);
		if($app->id != "")
			$app_where = " AND appName = " . $db->quote($app->sparkle_name);		
	}

	// OS Versions
	$data = percentages("sparkle", "osVersion", 5);
	$chart = new gPieChart();
	$chart->setTitle("Mac OS X Version (Minor)");
	$chart->addDataSet($data['data']);
	$chart->valueLabels = $data['labels'];
	$os_img = $chart->getUrl();

	// Major OS Versions
	$tmp = percentages("sparkle", "osVersion");
	$data = array();
	for($i = 0; $i < count($tmp['data']); $i++)
	{
		$version = substr($tmp['labels'][$i], 0, 4);
		if(!isset($data[$version])) $data[$version] = 0;
		$data[$version] += $tmp['data'][$i];
	}
	$total = array_sum($data);
	$labels = array();
	foreach($data as $k => $v) $labels[] = "$k (" . round($v / $total * 100, 1) . '%)';	
	$chart = new gPieChart();
	$chart->setTitle("Mac OS X Version (Major)");
	$chart->addDataSet($data);
	$chart->valueLabels = $labels;
	$mos_img = $chart->getUrl();

	// RAM
	$data = percentages("sparkle", "ramMB", 5);
	$chart = new gPieChart();
	$chart->setTitle("RAM");
	$chart->addDataSet($data['data']);
	$chart->valueLabels = $data['labels'];
	$ram_img = $chart->getUrl();

	// Languages
	$data = percentages("sparkle", "lang", 5);
	$chart = new gPieChart();
	$chart->setTitle("Language");
	$chart->addDataSet($data['data']);
	$chart->valueLabels = $data['labels'];
	$lang_img = $chart->getUrl();

	function percentages($table, $column, $num = null)
	{
		global $db, $app_where;
		
		$rows  = $db->getRows("SELECT COUNT(*) as v, $column as k FROM $table WHERE is_blank = 0 $app_where GROUP BY $column ORDER BY v DESC");
		$total = $db->getValue("SELECT COUNT(*) FROM $table WHERE is_blank = 0 $app_where");

		$data = array();
		$labels = array();
		foreach($rows as $row)
		{
			$data[]   = round($row['v'] / $total * 100, 1);
			$labels[] = $row['k'] . ' (' . round($row['v'] / $total * 100, 1) . '%)';
		}

		if(!is_null($num) && ($num < count($data)))
		{
			$full_data = $data;
			$data      = array_slice($full_data, 0, $num);
			$labels    = array_slice($labels, 0, $num);
			$other     = array_sum(array_slice($full_data, $num));
			if($other > 0)
			{
				$data[]   = $other;
				$labels[] = 'Other (' . array_sum(array_slice($full_data, $num)) . '%)';
			}
		}

		return array("data" => $data, "labels" => $labels);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>General Stats | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<script type="text/javascript" charset="utf-8">
		$(function() {
			$('#app_id').change(function() {
				window.location = 'stats.php?app_id=' + $('#app_id').val();
			});	
		});
		
	</script>
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li class='selected'><a href='<?PHP WEBROOT();?>stats.php'>General</a></li>
			<li><a href='<?PHP WEBROOT();?>sparkle-logs.php'>Sparkle Logs</a></li>
		</ul>
	</div>

	<div id="contain">
		<h2>Sparkle Stats</h2>
		<p>Application:
			<select name="app_id" id="app_id">
				<option value="">All</option>
				<?PHP echo get_options('apps', 'id', 'name', isset($_GET['app_id']) ? $_GET['app_id'] : null); ?>
			</select>
		</p>
		<p>
			<img src="<?PHP echo $mos_img;?>" />
			<img src="<?PHP echo $os_img;?>" />
		</p>
		<p>
			<img src="<?PHP echo $lang_img;?>" />
			<img src="<?PHP echo $ram_img;?>" />
		</p>
	</div>
</body>
</html>
